Member Variable
   HOME

TheInfoList



OR:

In
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
, a member variable (sometimes called a member
field Field may refer to: Expanses of open ground * Field (agriculture), an area of land used for agricultural purposes * Airfield, an aerodrome that lacks the infrastructure of an airport * Battlefield * Lawn, an area of mowed grass * Meadow, a grass ...
) is a
variable Variable may refer to: * Variable (computer science), a symbolic name associated with a value and whose associated value may be changed * Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many ...
that is associated with a specific
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
, and accessible for all its
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
(''member functions''). In
class-based programming Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance occurs via defining ''classes'' of objects, instead of inheritance occurring via the objects alone (compare prototy ...
languages, these are distinguished into two types: ''
class variable In class-based, object-oriented programming, a class variable is a variable defined in a class of which a single copy exists, regardless of how many instances of the class exist. A class variable is not an instance variable. It is a special ...
s'' (also called ''static member variables''), where only one copy of the variable is shared with all instances of the
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
; and ''
instance variable In class-based, object-oriented programming, an instance variable is a variable defined in a class (i.e. a member variable), for which each instantiated object of the class has a separate copy, or instance. An instance variable has similariti ...
s'', where each instance of the class has its own independent copy of the variable.


For Examples


C++

class Foo ; int main ()


Java

public class Program public class Foo


Python

class Foo: def __init__(self): self._bar = 0 @property def bar(self): return self._bar @bar.setter def bar(self, new_bar): self._bar = new_bar f = Foo() f.bar = 100 print(f.bar)


Common Lisp

(defclass foo () (bar)) (defvar f (make-instance 'foo)) (setf (slot-value f 'bar) 100) (print (slot-value f 'bar))


Ruby

/* Ruby has three member variable types: class, class instance, and instance. */ class Dog # The class variable is defined within the class body with two at-signs # and describes data about all Dogs *and* their derived Dog breeds (if any) @@sniffs = true end mutt = Dog.new mutt.class.sniffs #=> true class Poodle < Dog # The "class instance variable" is defined within the class body with a single at-sign # and describes data about only the Poodle class. It makes no claim about its parent class # or any possible subclass derived from Poodle @sheds = false # When a new Poodle instance is created, by default it is untrained. The 'trained' variable # is local to the initialize method and is used to set the instance variable @trained # An instance variable is defined within an instance method and is a member of the Poodle instance def initialize(trained = false) @trained = trained end def has_manners? @trained end end p = Poodle.new p.class.sheds #=> false p.has_manners? #=> false


PHP

foo = 10; // Prints 10. echo $example->foo;


Lua

--region example --- @class example_c --- @field foo number Example "member variable". local example_c = local example_mt = --- Creates an object from example. --- @return example_c function example_c.new(foo) -- The first table argument is our object's member variables. -- In a Lua object is a metatable and its member variables are table key-value pairs. return setmetatable(, example_mt) end --endregion -- Create an example object. -- Set the "foo" member variable to 5. local example = example_c.new(5) -- Overwrite the "foo" member variable to 10. example.foo = 10 -- Prints 10. print(example.foo)


See also

*
Global variable In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. The set of all global variables is known as the ''global environment'' or ''global s ...
*
Local variable In computer science, a local variable is a variable that is given ''local scope''. A local variable reference in the function or block in which it is declared overrides the same variable name in the larger scope. In programming languages with o ...
*
Property (programming) A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method. The syntax for reading and writing of properties is like for fields, but pr ...


References

{{Reflist Object-oriented programming Variable (computer science) Articles with example Python (programming language) code